package com.css.fxfzypg.modules.baseData.repository.impl;

import com.css.fxfzypg.modules.baseData.entity.*;
import com.css.fxfzypg.modules.baseData.repository.BasicDataRepository;
import com.css.fxfzypg.modules.preAssessResult.entity.Vulnerability;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author Dbj
 * @date 2022年10月16日  10:23
 */
@Repository
public class BasicDataRepositoryImpl implements BasicDataRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public List<TaskPbe> getAllPbeByTaskId(String taskId) {
        String sql = "select * from setting_seismic_pbe where task_id='"+taskId+"'";
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(TaskPbe.class));
    }

    @Override
    public void save(CenterResult centerResult) {
        jdbcTemplate.update("insert into setting_seismic_center_result(uuid,task_id,earthquake_id,influenceflie_id,pbe_id,structure_type,basic_area,basic,slight_area,slight,mid_area,mid,serious_area,serious,damage_area,damage,area,population) " +
                        "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                centerResult.getUuid(),centerResult.getTaskId(),centerResult.getEarthquakeId(),centerResult.getInfluenceflieId(),
                centerResult.getPbeId(),centerResult.getStructureType(),centerResult.getBasicArea(),centerResult.getBasic(),centerResult.getSlightArea(),centerResult.getSlight(),centerResult.getMidArea(),
                centerResult.getMid(),centerResult.getSeriousArea(),centerResult.getSerious(),centerResult.getDamageArea(),centerResult.getDamage(),centerResult.getArea(),centerResult.getPopulation());

    }

    @Override
    public void updateTaskPbe(TaskPbe pbe) {
        StringBuilder sql = new StringBuilder("update setting_seismic_pbe set");
        if(PlatformObjectUtils.isNotEmpty(pbe.getDeathPercent())){
            sql.append(" death_percent='").append(pbe.getDeathPercent()).append("',");
        }
        if(PlatformObjectUtils.isNotEmpty(pbe.getDeathPerson())){
            sql.append(" death_person='").append(pbe.getDeathPerson()).append("',");
        }
        if(PlatformObjectUtils.isNotEmpty(pbe.getInjuryPerson())){
            sql.append(" injury_person='").append(pbe.getInjuryPerson()).append("',");
            //condition.append(pbe.getInjuryPerson()).append(",");
        }
        if(PlatformObjectUtils.isNotEmpty(pbe.getStructureLosses())){
            sql.append(" structure_losses='").append(pbe.getStructureLosses()).append("',");
            //condition.append(pbe.getStructureLosses()).append(",");
        }
        if(PlatformObjectUtils.isNotEmpty(pbe.getIndoorLosses())){
            sql.append(" indoor_losses='").append(pbe.getIndoorLosses()).append("',");
            //condition.append(pbe.getIndoorLosses()).append(",");
        }
        if(PlatformObjectUtils.isNotEmpty(pbe.getStructureDirectLosses())){
            sql.append(" structure_direct_losses='").append(pbe.getStructureDirectLosses()).append("',");
            //condition.append(pbe.getIndirectLosses()).append(",");
        }
        if(PlatformObjectUtils.isNotEmpty(pbe.getLifeLosses())){
            sql.append(" life_losses='").append(pbe.getLifeLosses()).append("',");
            //condition.append(pbe.getLifeLosses()).append(",");
        }
        if(PlatformObjectUtils.isNotEmpty(pbe.getOtherLosses())){
            sql.append(" other_losses='").append(pbe.getOtherLosses()).append("',");
            //condition.append(pbe.getOtherLosses()).append(",");
        }
        if(PlatformObjectUtils.isNotEmpty(pbe.getDirectLosses())){
            sql.append(" direct_losses='").append(pbe.getDirectLosses()).append("',");
            //condition.append(pbe.getDirectLosses());
        }
        String s = sql.toString();
        s = s.substring(0,s.length()-1);
        StringBuilder builder = new StringBuilder(s);
        builder.append(" where task_id='").append(pbe.getTaskId()).append("'");
        builder.append(" and earthquake_id='").append(pbe.getEarthquakeId()).append("'");
        builder.append(" and influenceflied_id='").append(pbe.getInfluencefliedId()).append("'");
        builder.append(" and pbe_id='").append(pbe.getPbeId()).append("'");
        jdbcTemplate.update(builder.toString());

    }

    @Override
    public void deleteCenterResultByTaskId(String taskId) {
        jdbcTemplate.update("delete from  setting_seismic_center_result where task_id=?",taskId);
    }

    @Override
    public List<CenterResult> getCenterResult(String taskId) {
        String sql = "select * from setting_seismic_center_result where task_id='"+taskId+"' order by pbe_id,structure_type";
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CenterResult.class));
    }

    @Override
    public List<CenterResult> getCenterResultByPbeId(String taskId, String pbeId) {
        String sql = "select * from setting_seismic_center_result where task_id='"+taskId+"' and pbe_id='"+pbeId+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CenterResult.class));
    }
    @Override
    public List<TaskPbe> getTaskPbeByPbeId(String taskId, String pbeId) {
        String sql = "select * from setting_seismic_pbe where task_id='"+taskId+"' and pbe_id='"+pbeId+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TaskPbe.class));
    }

    @Override
    public void insertTaskPbe(TaskPbe pbe) {
//        jdbcTemplate.update("insert into setting_seismic_pbe(uuid,task_id,earthquake_id,influenceflied_id,pbe_id,death_percent,death_person,injury_person  ,structure_losses ,indoor_losses,structure_direct_losses,life_losses,other_losses,direct_losses) " +
//                        "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
//                pbe.getUuid(),pbe.getTaskId(),pbe.getEarthquakeId(),pbe.getInfluencefliedId(),
//                pbe.getPbeId(),pbe.getDeathPercent(),pbe.getDeathPerson(),pbe.getInjuryPerson(),pbe.getStructureLosses(),pbe.getIndoorLosses(),pbe.getStructureDirectLosses(),
//                pbe.getLifeLosses(),pbe.getOtherLosses(),pbe.getDirectLosses());
        jdbcTemplate.update("insert into setting_seismic_pbe(uuid,task_id,earthquake_id,influenceflied_id,pbe_id,death_percent,death_person,injury_person) " +
                        "values(?,?,?,?,?,?,?,?)",
                pbe.getUuid(),pbe.getTaskId(),pbe.getEarthquakeId(),pbe.getInfluencefliedId(),
                pbe.getPbeId(),pbe.getDeathPercent(),pbe.getDeathPerson(),pbe.getInjuryPerson());
    }

    @Override
    public void deleteSettingSeismicPbeByTaskId(String taskId) {
        String sql = "delete from setting_seismic_pbe where task_id='"+taskId+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void deleteSettingSeismicPbeEconomicByTaskId(String taskId) {
        String sql = "delete from setting_seismic_pbe_economic where task_id='"+taskId+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void insertTaskPbeEconomic(TaskPbeEconomic taskPbeEconomic) {
        jdbcTemplate.update("insert into setting_seismic_pbe_economic(uuid,task_id,earthquake_id,influenceflied_id,pbe_id,structure_losses ,indoor_losses,structure_direct_losses,life_losses,other_losses,direct_losses) " +
                        "values(?,?,?,?,?,?,?,?,?,?,?)",
                taskPbeEconomic.getUuid(),taskPbeEconomic.getTaskId(),taskPbeEconomic.getEarthquakeId(),taskPbeEconomic.getInfluencefliedId(),
                taskPbeEconomic.getPbeId(),taskPbeEconomic.getStructureLosses(),taskPbeEconomic.getIndoorLosses(),taskPbeEconomic.getStructureDirectLosses(),
                taskPbeEconomic.getLifeLosses(),taskPbeEconomic.getOtherLosses(),taskPbeEconomic.getDirectLosses());
    }

    @Override
    public int getPbeByTaskId(String taskId) {
        String sql = "select count(1) from setting_seismic_pbe where task_id='"+taskId+"'";
        int count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

    @Override
    public int getEconomicByTaskId(String taskId) {
        String sql = "select count(1) from setting_seismic_pbe_economic where task_id='"+taskId+"'";
        int count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

    @Override
    public List<CenterSumResult> getCenterSumResult(String taskId) {
        String sql = "select task_id,earthquake_id,influenceflie_id,pbe_id,area,population,sum(damage_area) as sum from setting_seismic_center_result where task_id='"+taskId+"' group by task_id,earthquake_id,influenceflie_id,pbe_id,area,population order by  task_id,earthquake_id,influenceflie_id,pbe_id,area,population";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CenterSumResult.class));
    }

    @Override
    public int getPbeLossesByTaskId(String taskId) {
        String sql = "select count(1) from data_pbe_losses where task_id='"+taskId+"'";
        int count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

    @Override
    public int getPbeInjuriesByTaskId(String taskId) {
        String sql = "select count(1) from data_pbe_injuries where task_id='"+taskId+"'";
        int count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }
}
